<template>
  <div class="wrap">
    <el-row>
      <el-col :span="24" class="header-wrap">
        <el-form :inline="true" :model="formInline" :rules="rules"  ref="formInline">
          <el-form-item label="真实姓名" prop="name">
            <el-input v-model="formInline.name" prefix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input v-model="formInline.phone" prefix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item label="订单号" prop="orderNum">
            <el-input v-model="formInline.orderNum" prefix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item label="订单状态" prop="state">
            <el-select v-model="formInline.state" placeholder="请选择订单状态">
              <el-option label="未放款" value="2"></el-option>
              <el-option label="已放款" value="3"></el-option>
              <el-option label="已到账" value="4"></el-option>
              <el-option label="全部" value=""></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search('formInline')">搜索</el-button>
            <el-button type="primary" @click="reset('formInline')">重置</el-button>
            <el-button type="primary"  icon="el-icon-printer" @click="outExe" >导出</el-button>
            <el-button type="warning" size="mini" @click="loan" :disabled="dis">放款</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!--表格-->
      <el-col :span="24">
        <el-table :data="landOrderList" :border="true" @selection-change="handleSelectionChange" style="width: 100%;min-width: 1200px;">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>

          <el-table-column label="订单号" align="center" min-width="140px;">
            <template slot-scope="scope">
              <span>{{ scope.row.orderNo }}</span>
            </template>
          </el-table-column>

          <el-table-column label="真实姓名" align="center">user
            <template slot-scope="scope">
              <span>{{ scope.row.realName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="手机号" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.phone }}</span>
            </template>
          </el-table-column>
          <el-table-column label="借款金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.loanAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column label="服务费" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.feeTotal }}</span>
            </template>
          </el-table-column>
          <el-table-column label="实际到账金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.actualAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column label="申请日期" :formatter="dateFormat" align="center" prop="createTime" min-width="140px;">

          </el-table-column>
          <el-table-column label="放款日期" :formatter="dateFormat" align="center" prop="loanTime" min-width="140px;">

          </el-table-column>
          <el-table-column label="到账日期" :formatter="dateFormat" align="center" prop="paymentTime" min-width="140px;">

          </el-table-column>
          <el-table-column label="状态" align="center" prop="status2">
            <template slot-scope="scope">
              <el-tag type="info" v-if="scope.row.status == 0">审核中</el-tag>
              <el-tag type="warning" v-if="scope.row.status == 1">审核失败</el-tag>
              <el-tag type="danger" v-if="scope.row.status == 2">未放款</el-tag>
              <el-tag type="primary" v-if="scope.row.status == 3">已放款</el-tag>
              <el-tag type="success" v-if="scope.row.status == 4">已到账</el-tag>
            </template>
          </el-table-column>

      <!--    <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="warning" size="mini" v-if="scope.row.status == 2" @click="loan(scope.$index, scope.row)">放款</el-button>
              <span v-else>&#45;&#45;</span>
            </template>
          </el-table-column>-->

        </el-table>
        <el-pagination
          class="top"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 15, 20, 30]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total='total'>
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script type="text/ecmascript-6">
  import {post} from '@/utils/http'
  export default {
    data() {
      let validateName = (rule, value, callback) => {
        if (value === '') {
          callback();
        } else {
          callback();
        }
      };
      let validatePhone = (rule, value, callback) => {
        if (value === '') {
          callback();
        } else {
          callback()
        }
      };
      return {
        dis: false,
        arr: [],
        lenarr: [],
        formInline: {
          name: '',
          phone: '',
          orderNum: '',
          state: ''
        },
        rules: {
          name: [
            { validator: validateName, trigger: 'blur' }
          ],
          phone: [
            { validator: validatePhone, trigger: 'blur' }
          ]
        },
        status:'',
        landOrderList: [],
        currentPage: 1,
        total: 0,
        size: 10,
      }
    },
    methods: {
      dateFormat(row,column) {
        let tableTime = row[column.property];
        if (tableTime == undefined) {
          return "";
        }
        return this.moment(tableTime).format("YYYY-MM-DD HH:mm:ss");
      },
      handleSelectionChange(val) {
       this.arr = val;
       var thisLength = this.arr.length;
       if(thisLength > 0){
         this.lenarr = this.arr[thisLength - 1];
         if(this.lenarr.status == 3) {
           this.$confirm('该条订单已放款', '提示', {
             type: 'warning'
           })
         }
       }
      },
      outExe() {
        // 导出execle
        this.excelData = this.arr
        this.excelData.map((val) => {

          switch (val.status) {
            case 0:
              val.status =  '审核中';
              break;
            case 1:
              val.status = '审核失败';
              break;
            case 2:
              val.status = '未放款';
              break;
            case 3:
              val.status = '已放款';
              break;
            case 4:
              val.status = '已到账';
              break;
          }
        })
        this.export2Excel()
        this.getList();
      },
      export2Excel() {
        var that = this;
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel');
          const tHeader = ['真实姓名','手机号','银行卡号','状态'];
          const filterVal = ['realName','phone','cardNo','status'];
          const list = that.excelData;
          const data = that.formatJson(filterVal, list);
          const test = data
          console.log(test);
          export_json_to_excel(tHeader, data, '放款订单excel');
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },

      search(formInline) {
        this.$refs[formInline].validate((vaild) => {
          if(vaild) {
            let para = {
              pageIndex: this.currentPage,
              pageSize: this.size
            }
            if(!this.formInline.name.replace(/\s/g, "") == '') {
              para.realName = this.formInline.name.replace(/\s/g, "")
            }
            if(!this.formInline.phone.replace(/\s/g, "") == '') {
              para.phone = this.formInline.phone.replace(/\s/g, "")
            }
            if(!this.formInline.orderNum.replace(/\s/g, "") == '') {
              para.orderNo = this.formInline.orderNum.replace(/\s/g, "")
            }
            if(!this.formInline.state == '') {
              para.status = this.formInline.state
            }
            post('/api/manager/v1/loan/getLendingOrderList',para)
              .then((response) => {
                this.landOrderList = response.data.data;
                this.total = response.data.totalCount;
                this.size = response.data.pageSize;
              })
          }
        })
      },
      checkAll() {

      },
      reset(formInline) {
        this.$refs[formInline].resetFields();
      },
      getList() {
        post('/api/manager/v1/loan/getLendingOrderList',{pageIndex: this.currentPage,pageSize: this.size})
          .then((response) => {
            this.landOrderList = response.data.data;
            this.total = response.data.totalCount;
            this.size = response.data.pageSize;
          })
      },
      handleSizeChange(val) {
        this.size = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.currentPage =val;
        this.getList();
      },
      loan() {
       let arr = []
        console.log(this.arr)
        this.arr.map((val) => {
          if(val.status == 2){
            arr.push(val.orderNo)
          }
        })
        this.$confirm('确认放款吗?', '提示', {
          type: 'warning'
        }).then(() => {
          post('/api/manager/v1/loan/batchLending',arr).then((response) => {
            if(response.data === 1) {
              this.$message({
                type: 'success',
                message: '放款成功!'
              });
            }else{
              this.$message({
                type: 'danger',
                message: '放款失败,请选择未放款订单!'
              });
            }
            this.getList()
          })
        }).catch(() => {
        // 点击取消
        });

      }
    },
    mounted(){
      this.getList();
    },
  }
</script>

<style lang='scss' scoped>
  .wrap {
    /*padding: 20px;*/
    .header-wrap{
      margin-bottom: 60px;
      background-color: #66BFBF;
      border-radius: 6px;
    }
    .el-form-item {
      margin-top: 22px;
      margin-left: 22px;
    }
    .top {
      margin-top: 10px;
      float: right;
    }
  }
</style>
